package 数组完成房屋租赁系统;

import java.util.ArrayList;
import java.util.Iterator;

public class HouseView {
    //主菜单类
    boolean loop =true;
    char key =' ';
    String name ="";
    String number ="";
    String address ="";
    double money=0.0;
    String status="";
    House house =null;
    int id=1;
    HouseService houseService =new HouseService();

    String details="编号\t房主\t电话\t地址\t月租\t房屋状态（出租/为出租）";

    public void mainView(){
        houseService.addDefault();

        while (loop) {
            System.out.println("========================房屋出租系统===========================");
            System.out.println("\t\t1.新 增 房 屋");
            System.out.println("\t\t2.查 找 房 屋");
            System.out.println("\t\t3.删 除 房 屋");
            System.out.println("\t\t4.修 改 房 屋 信 息");
            System.out.println("\t\t5.房 屋 列 表");
            System.out.println("\t\t6.退    出");
            System.out.println("请进行选择：(1~6)");

            key=Utility.readChar();
            switch (key){
                case '1':
                    //新增房屋界面
                    addHouse();
                    break;
                case '2':
                    //查找房屋界面
                    System.out.println("请输入要查房子的编号");
                    id=Utility.readInt();
                    queryHouse(id -1);
                    break;
                case '3':
                    //删除房屋界面
                    System.out.println("请选择要删除的房子编号 输入（-1）退出");
                    id=Utility.readInt();
                    if (id==-1){
                        break;
                    }
                    System.out.println("真的要删除吗？Y/N");
                    char a=Utility.readConfirmSelection();
                    if (a=='Y'){
                        deleteHouse(id -1);
                    }
                    break;
                case '4':
                    //修改房屋界面
                    System.out.println("请输入要修改的房屋编号（-1）退出");
                    id=Utility.readInt();
                    if (id==-1){
                        break;
                    }
                    updateHouse(id-1);
                    break;
                case '5':
                    //房屋列表界面
                    listHouse();
                    break;
                case '6':
                    //退出界面
                    a=Utility.readConfirmSelection();
                    if (a=='Y'){
                        loop=false;
                    }
                    break;
            }
        }
    }

    /**
        添加房屋
     */
    public void addHouse(){
        System.out.println("========================房屋出租系统===========================");
        System.out.println("----------添加房屋信息----------");
        System.out.println("姓名：");
        name=Utility.readString(10);
        System.out.println("电话：");
        number=Utility.readString(11);
        System.out.println("地址：");
        address=Utility.readString(10);
        System.out.println("租金");
        money=Utility.readInt();
        System.out.println("房屋状态(未出租/已出租)");
        status=Utility.readString(10);
        house=new House(name,number,address,(int)money,status);
        if (houseService.add(house)){
            System.out.println("------添加完成------");
        }else {
            System.out.println("------添加失败------");
        }
    }

    /**
     * @param id 通过id查找房屋
     */
    public void queryHouse(int id){
        house =houseService.findById(id);
            String str ="";
        if (house!=null){
            str += ++id +"\t\t"+house.getName()+"\t\t"+ house.getNumber()+"\t\t"+house.getAddress()
                    +"\t\t"+house.getMoney()+"\t\t"+ house.getStatus();
            System.out.println(str);
        }else {
            System.out.println("------房屋信息不存在------");
            return;
        }
//        if (house == null) {
//            System.out.println("=============修改房屋信息编号不存在..============");
//            return;
//        }
//        String str = "";
//        if (house != null) {
//            str += ++id + "\t\t" + house.getName() + "\t\t" + house.getNumber()
//                    + "\t\t" + house.getAddress() + "\t\t" + house.getMoney() + "\t\t" + house.getStatus();
//            System.out.println(str);
//        }
    }

    /**
     * @param id 通过id进行删除
     */
    public void deleteHouse(int id){
        ArrayList<House> houses=houseService.gteHouse();
        houseService.delete(id);
        System.out.println("------删除完成------");
    }

    /**
     * @param id  通过id进行更新房屋信息
     */
    public void updateHouse(int id){
        House house=houseService.findById(id);
        if (house==null){
            System.out.println("------修改房屋信息的编号不存在------");
            return;
        }
        System.out.println("姓名:("+house.getName()+"): ");
        String name=Utility.readString(8,"");//如果用户不想修改此条信息，直接回车表示不修改，默认”“
        if (!"".equals(name)){
            house.setName(name);
        }
        System.out.println("电话:("+house.getNumber()+"): ");
        String number=Utility.readString(11,"");
        if (!"".equals(number)){
            house.setNumber(number);
        }
        System.out.println("地址:("+house.getAddress()+"): ");
        String address=Utility.readString(18,"");
        if (!"".equals(address)){
            house.setAddress(address);
        }
        System.out.println("租金:("+house.getMoney()+"): ");
        int rent =Utility.readInt(-1);
        if (rent!=-1){
            house.setMoney(rent);
        }
        System.out.println("出租状态：（"+house.getStatus()+"): ");
        String status=Utility.readString(3,"");
        if (!"".equals(status)){
            house.setStatus(status);
        }
    }
    public void listHouse(){
        System.out.println("--------------房屋信息--------------");
        ArrayList<House> houses =houseService.gteHouse();
        Iterator<House> iterator=houses.iterator();
        String details="编号\t\t房主\t\t电话\t\t\t\t地址\t\t\t月租\t\t\t状态(出租/未出租)";
        id=0;
        while (iterator.hasNext()){

            House next=iterator.next();
            ++id;
            details+= "\n" + id + "\t\t" + next.getName() + "\t\t" + next.getNumber() + "\t\t"
                    + next.getAddress() + "\t\t" + next.getMoney() + "\t\t" + next.getStatus();
        }
        System.out.println(details);
        System.out.println("--------------房屋信息展示完毕--------------");
    }

}
